﻿using System;
using System.Drawing;
using System.Windows.Forms;


namespace Marmat.Forms
{
    public partial class TreeNodeCollectionForm : Skin.Form
    {
        public TreeNodeCollectionForm(TreeView treeView)
        {

            // 此调用是设计器所必需的。
            this.InitializeComponent();

            // 在 InitializeComponent() 调用之后添加任何初始化。
            this.TreeView1.Nodes.AddRange(treeView.Nodes);
            ContextMenuStrip1.Name = "ContextMenuStrip1";
            Button3.Name = "Button3";
            TreeView1.Name = "TreeView1";
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            if (sender is Control control)
            {
                Point p = control.PointToScreen(new Point(0, 0));
                this.ToolStripMenuItem2.Enabled = this.TreeView1.Nodes.Count > 0;
                this.ContextMenuStrip1.Show(p.X, p.Y + control.Height);
            }
        }

        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.PropertyGrid1.SelectedObject = e.Node;
        }

        private void ContextMenuStrip1_ItemClicked(object sender, System.Windows.Forms.ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Text ?? "")
            {
                case "添加根":
                    {
                        if (this.TreeView1.Nodes.Count == 0)
                        {
                            this.PropertyGrid1.SelectedObject = this.TreeView1.Nodes.Add("节点1", "");
                        }
                        else if (this.TreeView1.SelectedNode is object && this.TreeView1.SelectedNode.Level == 0)
                        {
                            this.PropertyGrid1.SelectedObject = this.TreeView1.Nodes.Add("节点", "");
                        }
                        else if (this.TreeView1.SelectedNode is object && this.TreeView1.SelectedNode.Parent is object)
                        {
                            this.PropertyGrid1.SelectedObject = this.TreeView1.SelectedNode.Parent.Nodes.Add("节点", "");
                        }

                        break;
                    }

                case "添加子级":
                    {
                        if (this.TreeView1.SelectedNode is object)
                        {
                            var nd = this.TreeView1.SelectedNode;
                            this.PropertyGrid1.SelectedObject = nd.Nodes.Add("节点" + this.TreeView1.GetNodeCount(true), "");
                        }

                        break;
                    }
            }
        }
    }
}